<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JsUnit Product Tests</title>
<link type="text/css" rel="Stylesheet" media="screen" href="../../stylesheets/styles.css"/>

<script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
<script src="../../dependencies/prototype.js" type="text/javascript"></script>
<script src="../../dependencies/inflection.js" type="text/javascript"></script>
<script src="../../resources.js" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript">

var BOBO_FIXTURE={	name: "Bobo", email:'bobo@gmail.com'};
var element;
var collection;
var people;
var mock_request;

Ajax.Request.prototype.initialize=function(url, options) {
    this.setOptions(options);
	mock_request={options:this.options,url:url};
}

Ajax.Updater.prototype.initialize=function(element, url, options) {
    this.setOptions(options);
	mock_request={element:element,options:this.options,url:url};
}

function setUp(){
	mock_request=null;
	collection=$('people');
	element=$('person_1');
	people=$RES('people');
}

function testDomSetup(){
	assertNotNull(collection);
	assertNotNull(element);
	assertEquals(1,collection.immediateDescendants().length);
}

function testIsResourceId(){
	assertTrue("/agreements",!Resource.isResourceId('agreements'));
	assertTrue("/agreements/1",Resource.isResourceId('agreement_1'));
	assertTrue("/agreements/12",Resource.isResourceId('agreement_12'));
	assertTrue("/agreements/1/parties",!Resource.isResourceId('agreement_1_parties'));
	assertTrue("/agreements/1/parties/2",Resource.isResourceId('agreement_1_party_2'));	
	assertTrue("/agreements/1/parties/2/events",!Resource.isResourceId('agreement_1_party_2_events'));
	assertTrue("/agreements/1/parties/2/events/4",Resource.isResourceId('agreement_1_party_2_event_4'));	
}
function testIdToPath(){
	assertEquals("/agreements",Resource.toPath('agreements'));
	assertEquals("/agreements/1",Resource.toPath('agreement_1'));
	assertEquals("/agreements/12",Resource.toPath('agreement_12'));
	assertEquals("/agreements/1/parties",Resource.toPath('agreement_1_parties'));
	assertEquals("/agreements/1/parties/2",Resource.toPath('agreement_1_party_2'));	
	assertEquals("/agreements/1/parties/2/events",Resource.toPath('agreement_1_party_2_events'));
	assertEquals("/agreements/1/parties/2/events/4",Resource.toPath('agreement_1_party_2_event_4'));	
}
function testPathToId(){
	assertEquals("agreements",Resource.toId('/agreements'));
	assertEquals("agreement_1",Resource.toId('/agreements/1'));
	assertEquals("agreement_12",Resource.toId('/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('/agreements/1/parties/2/events/4'));	
}
function testUrlToId(){
	assertEquals("agreements",Resource.toId('http://localhost:3000/agreements'));
	assertEquals("agreement_1",Resource.toId('http://localhost:3000/agreements/1'));
	assertEquals("agreement_12",Resource.toId('http://localhost:3000/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('http://localhost:3000/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('http://localhost:3000/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('http://localhost:3000/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('http://localhost:3000/agreements/1/parties/2/events/4'));	
	
	assertEquals("agreements",Resource.toId('http://test.com:3000/agreements'));
	assertEquals("agreement_1",Resource.toId('http://test.com:3000/agreements/1'));
	assertEquals("agreement_12",Resource.toId('http://test.com:3000/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('http://test.com:3000/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('http://test.com:3000/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('http://test.com:3000/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('http://test.com:3000/agreements/1/parties/2/events/4'));	

	assertEquals("agreements",Resource.toId('http://test.com/agreements'));
	assertEquals("agreement_1",Resource.toId('http://test.com/agreements/1'));
	assertEquals("agreement_12",Resource.toId('http://test.com/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('http://test.com/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('http://test.com/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('http://test.com/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('http://test.com/agreements/1/parties/2/events/4'));	

	assertEquals("agreements",Resource.toId('http://test/agreements'));
	assertEquals("agreement_1",Resource.toId('http://test/agreements/1'));
	assertEquals("agreement_12",Resource.toId('http://test/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('http://test/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('http://test/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('http://test/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('http://test/agreements/1/parties/2/events/4'));	

	assertEquals("agreements",Resource.toId('https://test.com:3000/agreements'));
	assertEquals("agreement_1",Resource.toId('https://test.com:3000/agreements/1'));
	assertEquals("agreement_12",Resource.toId('https://test.com:3000/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('https://test.com:3000/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('https://test.com:3000/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('https://test.com:3000/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('https://test.com:3000/agreements/1/parties/2/events/4'));	

	assertEquals("agreements",Resource.toId('https://test.com/agreements'));
	assertEquals("agreement_1",Resource.toId('https://test.com/agreements/1'));
	assertEquals("agreement_12",Resource.toId('https://test.com/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('https://test.com/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('https://test.com/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('https://test.com/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('https://test.com/agreements/1/parties/2/events/4'));	
	assertEquals("agreements",Resource.toId('https://test/agreements'));
	assertEquals("agreement_1",Resource.toId('https://test/agreements/1'));
	assertEquals("agreement_12",Resource.toId('https://test/agreements/12'));
	assertEquals("agreement_1_parties",Resource.toId('https://test/agreements/1/parties'));
	assertEquals("agreement_1_party_2",Resource.toId('https://test/agreements/1/parties/2'));	
	assertEquals("agreement_1_party_2_events",Resource.toId('https://test/agreements/1/parties/2/events'));
	assertEquals("agreement_1_party_2_event_4",Resource.toId('https://test/agreements/1/parties/2/events/4'));	
	
}

function testResourceDecoration(){
	assertNotNull(people);
	assertEquals(1,people.count());
	assertEquals('person',people.instance_name);
}

function testResourceShortCutById(){
	var collection=people;
	assertNotNull('not null',collection);
	assertEquals('instance name','person',collection.instance_name);
	assertEquals('collection name','people',collection.collection_name);
	assertEquals('prefix','',collection.resource_prefix);
	assertEquals('path','/people',collection.url());
	assertEquals('has one person',1,collection.count());
}
function testResourceShortCutByElement(){
	var collection=$RES($('people'));
	assertNotNull('not null',collection);
	assertEquals('instance name','person',collection.instance_name);
	assertEquals('collection name','people',collection.collection_name);
	assertEquals('prefix','',collection.resource_prefix);
	assertEquals('path','/people',collection.url());
	assertEquals('has one person',1,collection.count());	
}
function testResourceShortCutByItself(){
	var collection=$RES(people);
	assertNotNull('not null',collection);
	assertEquals('instance name','person',collection.instance_name);
	assertEquals('collection name','people',collection.collection_name);
	assertEquals('prefix','',collection.resource_prefix);
	assertEquals('path','/people',collection.url());
	assertEquals('has one person',1,collection.count());	
}
function testResourceShortCutByPath(){
	var collection=$RES('/teams');
	assertNotNull('not null',collection);
	assertEquals('instance name','team',collection.instance_name);
	assertEquals('collection name','teams',collection.collection_name);
	assertEquals('prefix','',collection.resource_prefix);
	assertEquals('path','/teams',collection.url());
	assertEquals('has one team',1,collection.count());	
}
function testResourceShortCutBySubPath(){
	var collection=$RES('/teams/1/members');
	assertNotNull('not null',collection);
	assertEquals('instance name','member',collection.instance_name);
	assertEquals('collection name','members',collection.collection_name);
	assertEquals('prefix','/teams/1',collection.resource_prefix);
	assertEquals('path','/teams/1/members',collection.url());
	assertEquals('has one member',1,collection.count());	
}

function testResourceShortCutByURL(){
	var collection=$RES('http://test.com/teams/2/projects');
	assertNotNull('not null',collection);
	assertEquals('instance name','project',collection.instance_name);
	assertEquals('collection name','projects',collection.collection_name);
	assertEquals('prefix','/teams/2',collection.resource_prefix);
	assertEquals('path','/teams/2/projects',collection.url());
	assertEquals('has one project',1,collection.count());	
}

function testResourceShortCutByURLwithShortId(){
	var collection=$RES('http://test.com/teams/1/projects');
	assertNotNull('not null',collection);
	assertEquals('instance name','project',collection.instance_name);
	assertEquals('collection name','projects',collection.collection_name);
	assertEquals('prefix','/teams/1',collection.resource_prefix);
	assertEquals('path','/teams/1/projects',collection.url());
	assertEquals('has one project',1,collection.count());	
}

function testShortCutFromForm(){
	var collection=$RES('project_form');
	assertNotNull('not null',collection);
	assertEquals('instance name','project',collection.instance_name);
	assertEquals('collection name','projects',collection.collection_name);
	assertEquals('prefix','/teams/3',collection.resource_prefix);
	assertEquals('path','/teams/3/projects',collection.url());
	assertEquals('has one project',1,collection.count());		
}

function testFindCollection(){
	var element=Resource.findCollection('team_2_projects');
	assertNotNull(element);
	assertEquals('team_2_projects',element.id);
	element=Resource.findCollection('team_3_projects');
	assertNotNull(element);
	assertEquals('team_3_projects',element.id);
	
	element=Resource.findCollection('team_1_projects');
	assertNotNull(element);
	assertEquals('projects',element.id);
	
	element=Resource.findCollection('not_here');
	assertNull(element);
	
	element=Resource.findCollection('company_1_team_4_projects');
	assertNotNull(element);
	assertEquals('company_1_team_4_projects',element.id);

	element=Resource.findCollection('company_1_team_2_projects');
	assertNotNull(element);
	assertEquals('team_2_projects',element.id);
	element=Resource.findCollection('company_1_team_3_projects');
	assertNotNull(element);
	assertEquals('team_3_projects',element.id);
	
	element=Resource.findCollection('company_1_team_1_projects');
	assertNotNull(element);
	assertEquals('projects',element.id);
}

function testFindPerson(){
	var person=people.find(1);
	assertEquals(element.id,person.id);
}

function testUrlGeneration(){
	assertEquals("/people",people.url());
}

function testReload(){
	people.reload();
	assertNotNull(mock_request);
	assertEquals('/people',mock_request.url);
	assertEquals('get',mock_request.options.method);
	assertEquals(people,mock_request.element);
}
function testCreate(){
	people.create(BOBO_FIXTURE);
	assertNotNull(mock_request);
	assertEquals('/people',mock_request.url);
	assertEquals('post',mock_request.options.method);
	assertEquals(Insertion.Bottom,mock_request.options.insertion);
}

</script>
</head>

<body>
	<h1>Unit tests for Resources</h1>
	<div id="people" class="people">
		<div id="person_1" class="person">
			<div class="name">Pelle</div>
			<div class="email">pelleb@gmail.com</div>
		</div>
	</div>

	<div id="teams" class="teams">
		<div id="team_1" class="team">
			<div class="name">Pelle</div>
			<div class="email">pelleb@gmail.com</div>
		</div>
	</div>
	<div id="members" class="members">
		<div id="member_1" class="member">
			<div class="name">Pelle</div>
			<div class="email">pelleb@gmail.com</div>
		</div>
	</div>
	
	<div id="projects" class="projects">
		<div id="project_1" class="project">
			<div class="name">Take over the world</div>
		</div>
	</div>
	<div id="team_2_projects" class="projects">
		<div id="project_2" class="project">
			<div class="name">Take over the world</div>
		</div>
	</div>
	<div id="team_3_projects" class="projects">
		<div id="project_3" class="project">
			<div class="name">Take over the world</div>
		</div>
	</div>
	<div id="company_1_team_4_projects" class="projects">
		<div id="project_4" class="project">
			<div class="name">Take over the world</div>
		</div>
	</div>
	<form id="project_form" action="http://test.com/teams/3/projects">
		
	</form>
</body>
</html>